package org.jeecg.modules.demo.ch.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.ch.Utils.OrderBomUtils;
import org.jeecg.modules.demo.ch.common.CommonMst;
import org.jeecg.modules.demo.ch.entity.DocumentManagement;
import org.jeecg.modules.demo.ch.service.IDocumentManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 文档管理
 * @Author: jeecg-boot
 * @Date:   2022-12-06
 * @Version: V1.0
 */
@Api(tags="文档管理")
@RestController
@RequestMapping("/ch/documentManagement")
@Slf4j
public class DocumentManagementController extends JeecgController<DocumentManagement, IDocumentManagementService> {
	@Autowired
	private IDocumentManagementService documentManagementService;

	/**
	 * 分页列表查询
	 *
	 * @param documentManagement
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "文档管理-分页列表查询")
	@ApiOperation(value="文档管理-分页列表查询", notes="文档管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<DocumentManagement>> queryPageList(DocumentManagement documentManagement,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   String uploadTime_begin,
								   String uploadTime_end,
								   String lastModificateTime_begin,
								   String lastModificateTime_end,
								   String lastViewTime_begin,
								   String lastViewTime_end,
								   HttpServletRequest req) {
		log.info("传来的参数documentManagement：{},uploadTime_begin:{},uploadTime_end:{},lastModificateTime_begin:{},lastModificateTime_end:{},lastViewTime_begin:{},lastViewTime_end:{}",documentManagement,uploadTime_begin,uploadTime_end,lastModificateTime_begin,lastModificateTime_end,lastViewTime_begin,lastViewTime_end);
//		QueryWrapper<DocumentManagement> queryWrapper = QueryGenerator.initQueryWrapper(documentManagement, req.getParameterMap());
		String title = documentManagement.getTitle();//标题
		String documentClassification = documentManagement.getDocumentClassification();//文档分类
		String versionNo = documentManagement.getVersionNo();//版本号
		String bodyOverview = documentManagement.getBodyOverview();//正文概述
		String uploader = documentManagement.getUploader();//上传者

		//版本号去V处理(数据库存的数字)
		if (StringUtils.hasLength(versionNo)){
			versionNo = versionNo.replace(CommonMst.DOCUMENT_MANAGEMENT_SYSTEM.VERSION_PREFIX, "");
			versionNo = versionNo.replace(CommonMst.DOCUMENT_MANAGEMENT_SYSTEM.VERSION_PREFIX_little, "");
		}

		LambdaQueryWrapper<DocumentManagement> managementLambdaQueryWrapper = new QueryWrapper<DocumentManagement>().lambda()
				.ge(StringUtils.hasLength(uploadTime_begin), DocumentManagement::getUploadTime, uploadTime_begin)
				.le(StringUtils.hasLength(uploadTime_end), DocumentManagement::getUploadTime, uploadTime_end)
				.ge(StringUtils.hasLength(lastModificateTime_begin), DocumentManagement::getLastModificateTime, lastModificateTime_begin)
				.le(StringUtils.hasLength(lastModificateTime_end), DocumentManagement::getLastModificateTime, lastModificateTime_end)
				.ge(StringUtils.hasLength(lastViewTime_begin), DocumentManagement::getLastViewTime, lastViewTime_begin)
				.le(StringUtils.hasLength(lastViewTime_end), DocumentManagement::getLastViewTime, lastViewTime_end)
				.like(StringUtils.hasLength(title), DocumentManagement::getTitle, title)
				.like(StringUtils.hasLength(documentClassification), DocumentManagement::getDocumentClassification, documentClassification)
				.like(StringUtils.hasLength(versionNo), DocumentManagement::getVersionNo, versionNo)
				.like(StringUtils.hasLength(bodyOverview), DocumentManagement::getBodyOverview, bodyOverview)
				.like(StringUtils.hasLength(uploader), DocumentManagement::getUploader, uploader);
		Page<DocumentManagement> page = new Page<>(pageNo, pageSize);
		IPage<DocumentManagement> pageList = documentManagementService.page(page, managementLambdaQueryWrapper);
		//版本处理  显示+V
		List<DocumentManagement> records = pageList.getRecords();
		records = records.stream().map(p -> {
			String versionNo1 = p.getVersionNo();
			if (StringUtils.hasLength(versionNo1) && !versionNo1.contains(CommonMst.DOCUMENT_MANAGEMENT_SYSTEM.VERSION_PREFIX)) {
				p.setVersionNo(CommonMst.DOCUMENT_MANAGEMENT_SYSTEM.VERSION_PREFIX + versionNo1);
			}
			return p;
		}).collect(Collectors.toList());
		pageList.setRecords(records);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param documentManagement
	 * @return
	 */
	@AutoLog(value = "文档管理-添加")
	@ApiOperation(value="文档管理-添加", notes="文档管理-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:document_management:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody DocumentManagement documentManagement) {
		//设置初始版本
		documentManagement.setVersionNo(CommonMst.DOCUMENT_MANAGEMENT_SYSTEM.VERSION);
		//设置上传时间
		documentManagement.setUploadTime(new Date());
		documentManagementService.save(documentManagement);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param documentManagement
	 * @return
	 */
	@AutoLog(value = "文档管理-编辑")
	@ApiOperation(value="文档管理-编辑", notes="文档管理-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:document_management:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DocumentManagement documentManagement) {
		//版本号+1
		LambdaQueryWrapper<DocumentManagement> lambdaQueryWrapper = new QueryWrapper<DocumentManagement>().lambda()
				.eq(DocumentManagement::getId, documentManagement.getId());
		DocumentManagement one = documentManagementService.getOne(lambdaQueryWrapper);
		if (one != null){
			documentManagement.setVersionNo(OrderBomUtils.setStringIfNotNull(Integer.parseInt(one.getVersionNo())+1));
		}
		//设置最近一次修改日期
		documentManagement.setLastModificateTime(new Date());
		//设置最近一次查询日期
		documentManagement.setLastViewTime(new Date());
		documentManagementService.updateById(documentManagement);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "文档管理-通过id删除")
	@ApiOperation(value="文档管理-通过id删除", notes="文档管理-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:document_management:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		documentManagementService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "文档管理-批量删除")
	@ApiOperation(value="文档管理-批量删除", notes="文档管理-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:document_management:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.documentManagementService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "文档管理-通过id查询")
	@ApiOperation(value="文档管理-通过id查询", notes="文档管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<DocumentManagement> queryById(@RequestParam(name="id",required=true) String id) {
		DocumentManagement documentManagement = documentManagementService.getById(id);
		if(documentManagement==null) {
			return Result.error("未找到对应数据");
		}
		//设置最近一次查询日期
		documentManagement.setLastViewTime(new Date());
		documentManagementService.updateById(documentManagement);
		return Result.OK(documentManagement);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param documentManagement
    */
    //@RequiresPermissions("org.jeecg.modules.demo:document_management:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DocumentManagement documentManagement) {
        return super.exportXls(request, documentManagement, DocumentManagement.class, "文档管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("document_management:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DocumentManagement.class);
    }

}
